//
//  NYPostWordToolBar.m
//  百思不得姐
//
//  Created by Apple on 15/8/22.
//  Copyright (c) 2015年 Neal. All rights reserved.
//

#import "NYPostWordToolBar.h"
#import "NYNavigationController.h"
#import "NYADDTagViewController.h"
#import "NYTagLabel.h"

@interface NYPostWordToolBar ()
@property (weak, nonatomic) IBOutlet UIView *topView;
/* 存放所有tagbael */
@property(nonatomic , strong) NSMutableArray *tagLabels;
/* plusbutton */
@property(nonatomic , weak) UIButton *plusbutton;

@end
@implementation NYPostWordToolBar
- (NSMutableArray *)tagLabels
{
    if (_tagLabels == nil) {
        _tagLabels = [NSMutableArray array];
    }
    return _tagLabels   ;
}

- (void)awakeFromNib
{
    UIButton *plusbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [plusbutton addTarget:self action:@selector(plusClick) forControlEvents:UIControlEventTouchUpInside];
    [plusbutton setImage:[UIImage imageNamed:@"tag_add_icon"] forState:UIControlStateNormal];
    [plusbutton sizeToFit];
    [self.topView addSubview:plusbutton];
    self.plusbutton = plusbutton;
    
    [self createTagLabel:@[@"搞笑",@"糗事",@"阁下"]];
}
- (void)plusClick
{
    NYWeakSelf;
    
    NYADDTagViewController *addtagvc = [[NYADDTagViewController alloc]init];
    addtagvc.tagblock = ^(NSArray *tags){
        [weakSelf createTagLabel:tags];
    };
    addtagvc.tags = [self.tagLabels valueForKeyPath:@"text"];
    
    NYNavigationController *nav = [[NYNavigationController alloc]initWithRootViewController:addtagvc];
    UIViewController *root = self.window.rootViewController;
    UIViewController *postword = root.presentedViewController;
    [postword presentViewController:nav animated:YES completion:^{
        
    }];

}
- (void)createTagLabel:(NSArray *)tags
{
    [self.tagLabels makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.tagLabels removeAllObjects];
    /**
     *  添加标签文本
     */
    for (int i = 0; i<tags.count; i++) {
        NYTagLabel *tagLabel = [[NYTagLabel alloc]init];
        tagLabel.text = tags[i];
        [self.topView addSubview:tagLabel];
        [self.tagLabels addObject:tagLabel];
        }
    
    [self setNeedsLayout];
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    for (int i = 0; i<self.tagLabels.count; i++) {
        NYTagLabel *tagLabel = self.tagLabels[i];
        if (i == 0 ) {
            tagLabel.x = 0;
            tagLabel.y = 0;
            
        } else {
            NYTagLabel *previousTagLabel = self.tagLabels[i-1];
            CGFloat rightWidth = self.topView.width - CGRectGetMaxX(previousTagLabel.frame)- SmallMargin;
            if (rightWidth >= tagLabel.width) {
                tagLabel.y = previousTagLabel.y;
                tagLabel.x = CGRectGetMaxX(previousTagLabel.frame) + SmallMargin;
            } else {
                tagLabel.y = CGRectGetMaxY(previousTagLabel.frame) + SmallMargin;
                tagLabel.x = 0;
                
            }
        }
    }
    
    //加号按钮
    NYTagLabel *lastTagLabel = self.tagLabels.lastObject;
    CGFloat rightWidth = self.topView.width - CGRectGetMaxX(lastTagLabel.frame)- SmallMargin;
    if (rightWidth >= self.plusbutton.width) {
        self.plusbutton.y = lastTagLabel.y;
        self.plusbutton.x = CGRectGetMaxX(lastTagLabel.frame) + SmallMargin;
    } else {
        self.plusbutton.y = CGRectGetMaxY(lastTagLabel.frame) + SmallMargin;
        self.plusbutton.x = 0;
        
    }
    
    self.topView.height = CGRectGetMaxY(self.plusbutton.frame) + SmallMargin;
    
    CGFloat oldH = self.height;
    
    self.height = CGRectGetMaxY(self.topView.frame) + SmallMargin + NYtagButtonHeight;
    self.y +=oldH - self.height;
}
@end
